package com.atguigu.gmall.product;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.*;

/**
 * @author 李旭
 * @date 2021/10/10 15:57
 * @Description:
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws Exception{
        //1:创建新线程
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,//核心线程
                10,//最大线程
                5,// 闲置线程的存活时间  == 最大线程-核心线程
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                //工厂
                //拒绝策略
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
        //【强制】 不允许使用下面工具类
        //Executors.newCachedThreadPool();

        //1:开启新线程
        CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        },threadPoolExecutor);

        //2:开启新线程
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "123";
        });
        System.out.print(stringCompletableFuture.get());
        //3:开启新线程
     /*   CompletableFuture<String> java = CompletableFuture.completedFuture("java");
        System.out.println(java.get());*/

    }
}
